home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 7_3.lha / 7_3 / 7_3d.c < prev    next >
Text File  |  1993-08-08  |  1KB  |  54 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / exercise 7.3
  6. / draw a circle
  7.  
  8. / draw a point, offset by the location 
  9. / of the center of the circle
  10. nline void put_circle_point(int x, int y, point center)
  11.  
  12.    put_point(x + center.x, y + center.y);
  13.  
  14.  
  15. / draw the point in all 8 octants
  16. tatic void draw_circle_points(point pt, point center)
  17.  
  18.    put_circle_point(pt.x, pt.y, center);
  19.    put_circle_point(pt.y, pt.x, center);
  20.    put_circle_point(pt.y, -pt.x, center);
  21.    put_circle_point(pt.x, -pt.y, center);
  22.    put_circle_point(-pt.x, -pt.y, center);
  23.    put_circle_point(-pt.y, -pt.x, center);
  24.    put_circle_point(-pt.y, pt.x, center);
  25.    put_circle_point(-pt.x, pt.y, center);
  26.  
  27.  
  28. / draw a circle using Bresenham's algorithm 
  29. / as described by Foley and Van Dam, 1982
  30. oid circle:: draw()
  31.  
  32.    point pt(0, radius);
  33.    int d = 3 - 2 * radius;
  34.  
  35.    while (pt.x < pt.y)
  36.        {
  37. draw_circle_points(pt, center);
  38.  
  39. if (d < 0)
  40.     d += 4 * pt.x + 6;
  41.  
  42. else
  43.     {
  44.     d += 4 * (pt.x - pt.y) + 10;
  45.     pt.y--;
  46.     }
  47.  
  48. pt.x++;
  49. }
  50.  
  51.    if (pt.x == pt.y)
  52.        draw_circle_points(pt, center);    
  53.  
  54.